package com.workerhelping.controller;

import com.workerhelping.constant.MQConstant;
import com.workerhelping.dto.RabbitMQDTO;
import com.workerhelping.elasticsearchEntity.ESIncomeVO;
import com.workerhelping.elasticsearchEntity.ESRecordVO;
import com.workerhelping.enums.IncomeCategoryEnum;
import com.workerhelping.enums.StatusEnum;
import com.workerhelping.feignClients.ESFeignClient;
import com.workerhelping.feignClients.MySQLFeignClient;
import com.workerhelping.feignClients.RabbitMQFeignClient;
import com.workerhelping.result.Result;
import com.workerhelping.result.ResultCodeEnum;
import com.workerhelping.vo.IncomeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/income")
public class IncomeController {

    @Autowired
    private MySQLFeignClient mySQLFeignClient;

    @Autowired
    private RabbitMQFeignClient rabbitMQFeignClient;

    @Autowired
    private ESFeignClient esFeignClient;

    /**
     * 创建表单 收入类别前端写死
     */

    /**
     * 创建收入信息
     *
     * @return
     */
    @PostMapping("/")
    public Result<String> add(@RequestBody IncomeVO incomeVO) {
        Result<Integer> saveResult = mySQLFeignClient.saveAndIncrease(incomeVO);
        Integer incomeId = saveResult.getData();
        ESIncomeVO esIncomeVO = new ESIncomeVO();
        BeanUtils.copyProperties(incomeVO, esIncomeVO);
        esIncomeVO.setId(incomeId);
        esIncomeVO.setCategory(IncomeCategoryEnum.getCategoryName(incomeVO.getIcId()));
        RabbitMQDTO rabbitMQDTO = new RabbitMQDTO(
                MQConstant.EXCHANGE_INCOME,
                MQConstant.ROUTINGKEY_INCOME_INSERT,
                esIncomeVO
        );
        rabbitMQFeignClient.sendMsg(rabbitMQDTO);
        if (!saveResult.isOk())
            return Result.build(null, ResultCodeEnum.SAVE_FAIL_COMMON);
        return Result.ok();
    }


    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Deprecated
//    @GetMapping(value = {"/"})
    public Result<Object> findAll(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        Result<Object> getResult = mySQLFeignClient.getAllIncome(pageNo, pageSize);
        if (!getResult.isOk())
            return Result.fail();
        Object incomes = getResult.getData();
        if (StringUtils.isEmpty(incomes))
            return Result.build(null, ResultCodeEnum.DATA_IS_EMPTY);
        return Result.ok(incomes);
    }

    /**
     * 聚合查询
     *
     * @param id        精确查询
     * @param startDate 区间查询 >=
     * @param endDate   区间查询 <=
     * @param year      精确查询
     * @param minPrice  区间查询>=
     * @param maxPrice  区间查询<=
     * @param category  精确查询
     * @return
     */
    @GetMapping("/")
    public Result<List<ESIncomeVO>> search(@RequestParam(value = "sId", required = false) Integer id,
                                           @RequestParam(value = "startDate", required = false) String startDate,
                                           @RequestParam(value = "endDate", required = false) String endDate,
                                           @RequestParam(value = "sYear", required = false) String year,
                                           @RequestParam(value = "minPrice", required = false) Double minPrice,
                                           @RequestParam(value = "maxPrice", required = false) Double maxPrice,
                                           @RequestParam(value = "sCategory", required = false) String category) {
        Result<List<ESIncomeVO>> recordsResult = esFeignClient.searchIncome(id, startDate, endDate, year, minPrice, maxPrice, category);
        if (!recordsResult.isOk())
            return Result.fail();
        List<ESIncomeVO> incomes = recordsResult.getData();
        if (incomes.isEmpty())
            return Result.build(null, ResultCodeEnum.DATA_IS_EMPTY);
        return Result.ok(incomes);
    }
}
